/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "DataAcquisition.h"
#include "hm_interaction.h"
#include "iwdg.h"
#include "temp_control.h"
#include "stm32f407_lee_conf.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for SuperVisor */
osThreadId_t SuperVisorHandle;
const osThreadAttr_t SuperVisor_attributes = {
  .name = "SuperVisor",
  .stack_size = 64 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};
/* Definitions for ControllTask */
osThreadId_t ControllTaskHandle;
const osThreadAttr_t ControllTask_attributes = {
  .name = "ControllTask",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityLow1,
};
/* Definitions for HIDTask */
osThreadId_t HIDTaskHandle;
const osThreadAttr_t HIDTask_attributes = {
  .name = "HIDTask",
  .stack_size = 168 * 4,
  .priority = (osPriority_t) osPriorityLow2,
};
/* Definitions for MeaAndSenTask */
osThreadId_t MeaAndSenTaskHandle;
const osThreadAttr_t MeaAndSenTask_attributes = {
  .name = "MeaAndSenTask",
  .stack_size = 64 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for MeaTemp */
osMessageQueueId_t MeaTempHandle;
const osMessageQueueAttr_t MeaTemp_attributes = {
  .name = "MeaTemp"
};
/* Definitions for DataAcquireMutex */
osMutexId_t DataAcquireMutexHandle;
const osMutexAttr_t DataAcquireMutex_attributes = {
  .name = "DataAcquireMutex"
};
/* Definitions for Control_Mutex */
osMutexId_t Control_MutexHandle;
const osMutexAttr_t Control_Mutex_attributes = {
  .name = "Control_Mutex"
};
/* Definitions for SysStatusFlag */
osEventFlagsId_t SysStatusFlagHandle;
const osEventFlagsAttr_t SysStatusFlag_attributes = {
  .name = "SysStatusFlag"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void supervisorT(void *argument);
void controlT(void *argument);
void hmiT(void *argument);
void measureAsendT(void *argument);

extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */
  /* Create the mutex(es) */
  /* creation of DataAcquireMutex */
  DataAcquireMutexHandle = osMutexNew(&DataAcquireMutex_attributes);

  /* creation of Control_Mutex */
  Control_MutexHandle = osMutexNew(&Control_Mutex_attributes);

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of MeaTemp */
  MeaTempHandle = osMessageQueueNew (5, sizeof(float), &MeaTemp_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of SuperVisor */
  SuperVisorHandle = osThreadNew(supervisorT, NULL, &SuperVisor_attributes);

  /* creation of ControllTask */
  ControllTaskHandle = osThreadNew(controlT, NULL, &ControllTask_attributes);

  /* creation of HIDTask */
  HIDTaskHandle = osThreadNew(hmiT, NULL, &HIDTask_attributes);

  /* creation of MeaAndSenTask */
  MeaAndSenTaskHandle = osThreadNew(measureAsendT, NULL, &MeaAndSenTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* creation of SysStatusFlag */
  SysStatusFlagHandle = osEventFlagsNew(&SysStatusFlag_attributes);

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_supervisorT */
/**
  * @brief  Function implementing the SuperVisor thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_supervisorT */
void supervisorT(void *argument)
{
  /* init code for USB_DEVICE */
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN supervisorT */
  /* Infinite loop */
  for(;;)
  {
	SV_TASK();
	HAL_IWDG_Refresh(&hiwdg);
    osDelay(1000);
    //HAL_IWDG_Refresh(&hiwdg);
  }
  /* USER CODE END supervisorT */
}

/* USER CODE BEGIN Header_controlT */
/**
* @brief Function implementing the ControllTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_controlT */
void controlT(void *argument)
{
  /* USER CODE BEGIN controlT */
  /* Infinite loop */
  for(;;)
  {
	  if(osEventFlagsWait(SysStatusFlagHandle, 0x40000, osFlagsWaitAny, osWaitForever)&0x80000000){
		  //出错！
	  }else{
		  Temerature_Control_Task();
		  osEventFlagsClear(SysStatusFlagHandle, 0x40000);
	  }

  }
  /* USER CODE END controlT */
}

/* USER CODE BEGIN Header_hmiT */
/**
* @brief Function implementing the HIDTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_hmiT */
void hmiT(void *argument)
{
  /* USER CODE BEGIN hmiT */
  /* Infinite loop */
  for(;;)
  {
	  if(osEventFlagsWait(SysStatusFlagHandle, 0x20000, osFlagsWaitAny, osWaitForever)&0x80000000){
		  //出错！
	  }else{
		  //LED1_ON();
		  //数据显示
		  CurrentDataShow();
		  osEventFlagsClear(SysStatusFlagHandle, 0x20000);
		  //LED1_OFF();
	  }
	  osDelay(1000);
  }
  /* USER CODE END hmiT */
}

/* USER CODE BEGIN Header_measureAsendT */
/**
* @brief Function implementing the MeaAndSenTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_measureAsendT */
void measureAsendT(void *argument)
{
  /* USER CODE BEGIN measureAsendT */
  /* Infinite loop */
  for(;;)
  {
	  //另加绝对延时功能
	  DataAcquireTask();
	  while(osEventFlagsSet(SysStatusFlagHandle, 0x20000)&0x80000000);
	  while(osEventFlagsSet(SysStatusFlagHandle, 0x40000)&0x80000000);
  }
  /* USER CODE END measureAsendT */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

